package com.simple.util.math;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: simple_tools
 * @description: 统计学工具类
 * @author: ChenWenLong
 * @create: 2019-11-04 13:44
 **/
@SuppressWarnings("unchecked")
public class StatisticsUtil {

    /**
     * 功能描述:
     * 〈获取中位数〉
     *
     * @params : [numberList]
     * @return : java.util.List<java.lang.Integer>
     * @author : cwl
     * @date : 2019/11/4 14:40
     */
    public static List<Number> getMedian(List<? extends Number> numberList){
        if(null == numberList || numberList.size() == 0){
            throw new RuntimeException("numberList is not be empty or null");
        }
        List<Number> resultList = new ArrayList<>();
        if(numberList.size() % 2 == 0){
            int a = numberList.size()/2;
            int b = numberList.size()/2 - 1;
            resultList.add(numberList.get(a));
            resultList.add(numberList.get(b));
        }else{
            resultList.add(numberList.get(numberList.size()/2));
        }
        return resultList;
    }

    /**
     * 功能描述:
     * 〈获取平均值〉
     *
     * @params : [numberList]
     * @return : java.util.List<java.lang.Number>
     * @author : cwl
     * @date : 2019/11/4 14:45
     */
    public static Number getAverage(List<? extends Number> numberList){
        if(null == numberList || numberList.size() == 0){
            throw new RuntimeException("numberList is not be empty or null");
        }
        double v = getSum(numberList).doubleValue();
        return v / numberList.size();
    }

    /**
     * 功能描述:
     * 〈求和〉
     *
     * @params : [numberList]
     * @return : java.util.List<java.lang.Number>
     * @author : cwl
     * @date : 2019/11/4 14:47
     */
    public static Number getSum(List<? extends Number> numberList){
        if(null == numberList || numberList.size() == 0){
            throw new RuntimeException("numberList is not be empty or null");
        }
        Number result = new Number() {
            @Override
            public int intValue() {
                return 0;
            }

            @Override
            public long longValue() {
                return 0;
            }

            @Override
            public float floatValue() {
                return 0;
            }

            @Override
            public double doubleValue() {
                return 0;
            }
        };
        long longResult = 0;
        int intResult = 0 ;
        double doubleResult = 0;
        float floatResult = 0;
        for (Number number : numberList) {
            if(number.longValue() != 0){
                longResult = longResult + number.longValue();
            }
            if(number.doubleValue() != 0){
                doubleResult = doubleResult + number.doubleValue();
            }
            if(number.intValue() != 0){
                intResult = intResult + number.intValue();
            }
            if(number.floatValue() != 0){
                floatResult = floatResult + number.floatValue();
            }
        }
        return longResult + intResult + doubleResult + floatResult;

    }


}
